package com.neuedu.filter;

import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.URI;

@Component
public class GatewayFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("进入过滤器");
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String url = request.getURI().getPath();

        if(url.indexOf("/sys-user/token")>=0){
            return chain.filter(exchange);
        }
        //System.out.println("111111111");
        String token = request.getHeaders().getFirst("token");
        System.out.println(token);
        //System.out.println("22222222222");

//        if(StringUtils.isBlank(token)){
//            return NoToken(response);
//        }
//        else{
//            String username = JWT.decode(token).getClaim("username").asString();
//            System.out.println(username);
//        }
        //1、获取url  request.getURI().getRawQuery(),获取到?号后面的
        URI uri = request.getURI();

        //2、涉及到StringBuilder
//        StringBuilder builder = new StringBuilder();
//        String oldQuery = uri.getRawQuery();
//        if(StringUtils.isNoneBlank(uri.getRawQuery())){
//           // builder.append(oldQuery).append("&");
//            builder.append(oldQuery);
//        }
//        //替换成jwt中解析出来的用户名字
//        //builder.append("username=" + "admin");
//        //3、重新构建新的URI
//        URI newuri = UriComponentsBuilder.fromUri(uri)
//                .replaceQuery(builder.toString())
//                .build(true).toUri();
//        //4、重构请求
//        ServerHttpRequest newrequest = exchange.getRequest()
//                .mutate().uri(newuri)
//                .build();
//
//
////        //URI uri = request.getURI();
////        //获取请求地址，根据地址判断是否有权限 /ums-user/list
////        String value = request.getPath().value();
////        //通过这个拿到请求头，获取token
////        HttpHeaders headers = request.getHeaders();
////        //拿出请求方式，如果是OPTION请求那么放行
////        String name = request.getMethod().name();
////        //输出请求方式+请求地址
////        System.out.println(name);
////        System.out.println(value);
//
//        /**
//         * 判断有权限，如果有权限，改转到哪个微服务就
//         * 转到哪个微服务
//         *
//         * 怎样通过
//         * return chain.filter(exchange);//通过
//         * 怎么不通过
//         *没有绝对意义的不通过
//         *
//         * */
//
//        //System.out.println(newrequest.getURI().getRawQuery());
//        return chain.filter(exchange.mutate().request(newrequest).build());

        return chain.filter(exchange);

    }

    private Mono<Void> NoToken(ServerHttpResponse response) {
        //通过responese对象返回内容，返回json
        //获取到响应头
        response.getHeaders().set("Content-type","application/json;charset=UTF-8");
//
//        //fastjosn对象
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("403","获取不到token");
//        jsonObject.put("username","张三");
        //DataBuffer
        DataBuffer buffer = response.bufferFactory().wrap(jsonObject.toJSONString().getBytes());
        //输出点什么东西,自定义输出内容 response.writeWith(Flux.just(buffer));
        return response.writeWith(Flux.just(buffer));
    }

    @Override
    //过滤器先进入那个，后进入那个的次序，多个过滤器的情况下
    public int getOrder() {
        return 1;
    }
}
